package com.spzx.cart.controller;

import com.spzx.cart.api.domain.CartInfo;
import com.spzx.cart.service.CartInfoService;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.InnerAuth;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.spzx.common.core.web.domain.AjaxResult.success;

/**
 * @author GAX
 * @date 2025-09-27
 */

@RestController
public class CartController {
    @Autowired
    private CartInfoService cartService;

    @Operation(summary = "当前登录用户加购")
    @GetMapping("addToCart/{skuId}/{num}")
    public AjaxResult addToCart(
            @PathVariable("skuId") Long skuId,
            @PathVariable("num") Integer num) {
        cartService.addToCart(skuId, num);
        return success();
    }

    @Operation(summary = "查询购物车商品列表")
    @GetMapping("cartList")
    public AjaxResult cartList() {
        List<CartInfo> cartInfoList = cartService.cartList();
        return success(cartInfoList);
    }

    @Operation(summary = "选中/取消选中状态")
    @GetMapping("checkCart/{skuId}/{isChecked}")
    public AjaxResult checkCart(
            @PathVariable("skuId") Long skuId,
            @PathVariable("isChecked") Integer isChecked) {
        cartService.checkCart(skuId, isChecked);
        return success();
    }

    @Operation(summary="更新购物车商品全部选中状态")
    @GetMapping("/allCheckCart/{isChecked}")
    public AjaxResult allCheckCart(
            // @Parameter(description="") 描述某个形参的作用，这个注解是 Swagger 提供的
            @Parameter(name = "isChecked", description = "是否选中 1:选中 0:取消选中", required = true)
            @PathVariable(value = "isChecked") Integer isChecked
    ){
        cartService.allCheckCart(isChecked);
        return success();
    }

    @Operation(summary = "删除购物车商品")
    @DeleteMapping("deleteCart/{skuId}")
    public AjaxResult deleteCart(@PathVariable("skuId") Long skuId) {
        cartService.deleteCart(skuId);
        return success();
    }

    @Operation(summary="清空购物车")
    @GetMapping("/clearCart")
    public AjaxResult clearCart(){
        cartService.clearCart();
        return success();
    }

    @Operation(summary = "查询用户购物车列表中的选中的购物项")
    @InnerAuth
    @GetMapping("getCartCheckedList")
    public R<List<CartInfo>> getCartCheckedList() {
        return R.ok(cartService.getCartCheckedList());
    }

    @Operation(summary="更新用户购物车列表价格")
    @InnerAuth
    @GetMapping("/updateCartPrice/{userId}")
    public R<Boolean> updateCartPrice(@PathVariable("userId") Long userId){
        return R.ok(cartService.updateCartPrice(userId));
    }

    @Operation(summary="删除用户购物车列表中选中商品列表")
    @InnerAuth
    @GetMapping("deleteCartCheckedList")
    public R<Boolean> deleteCartCheckedList(){
        return R.ok(cartService.deleteCartCheckedList());
    }
}